package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.event.EventInfoLogic;
import cn.topevery.garden.entity.bo.EventInfoBo;
import cn.topevery.garden.entity.dto.event.EventDelayApplyDTO;
import cn.topevery.garden.entity.dto.event.EventDelayAuditDTO;
import cn.topevery.garden.entity.dto.event.EventInfoDTO;
import cn.topevery.garden.entity.dto.flow.BackActPara;
import cn.topevery.garden.entity.dto.flow.SendActPara;
import cn.topevery.garden.entity.query.EventDelayApplyQuery;
import cn.topevery.garden.entity.query.EventInfoQuery;
import cn.topevery.garden.entity.vo.event.ActivityInstanceVO;
import cn.topevery.garden.entity.vo.event.EventDelayApplyVO;
import cn.topevery.garden.entity.vo.event.EventInfoVO;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 案件表
 *
 * @author code-generate
 * @date 2022-08-23
 */
@Api(tags = "日常检查案件表")
@RestController
@RequestMapping("/eventInfo")
public class EventInfoController extends BaseController {

    @Autowired
    EventInfoLogic eventInfoLogic;

    @ApiOperation("案件上报")
    @PostMapping("/saveEvent")
    public JsonResult<?> saveEvent(@Valid @RequestBody EventInfoDTO dto) {
        return eventInfoLogic.saveEvent(getUser(), dto);
    }

    @ApiOperation("修改案件信息")
    @PostMapping("updateEvent")
    public JsonResult<?> updateEvent(@Valid @RequestBody EventInfoBo eventInfoBo) {
        return eventInfoLogic.updateEvent(getUser().userId, eventInfoBo);
    }

    @ApiOperation("案件办理")
    @PostMapping("/process")
    public JsonResult<?> processEvent(@RequestBody SendActPara sendActPara) {
        return eventInfoLogic.processEvent(getUser(), sendActPara);
    }

    @ApiOperation("获取待办案件分页页列表")
    @PostMapping("/getEventTodoList")
    public JsonResult<DataPager<EventInfoVO>> getEventTodoList(@RequestBody EventInfoQuery query) {
        return JsonResult.ok(eventInfoLogic.getEventTodoList(query, getUser()));
    }

    @ApiOperation("获取待办案件详细信息")
    @GetMapping("/getToDoEventDetail")
    public JsonResult<?> getToDoEventDetail(@RequestParam String evtId, @RequestParam String curActId) throws Exception {
        return eventInfoLogic.getToDoEventDetail(evtId, curActId, getUser().userId);
    }

    @ApiOperation("获取下一环节")
    @GetMapping("/getNextActivity")
    public JsonResult<?> getNextActivity(@RequestParam String actInstId, @RequestParam Integer sectionId) {
        return eventInfoLogic.getNextActivity(actInstId, sectionId);
    }

    @ApiOperation("案件查询")
    @PostMapping("/getEventSearchList")
    public JsonResult<DataPager<EventInfoVO>> getEventSearchList(@RequestBody EventInfoQuery query) {
        DataPager<EventInfoVO> page = eventInfoLogic.getEventSearchList(query, getUser());
        return JsonResult.ok(page);
    }

    @ApiOperation("巡检案件回退")
    @PostMapping("/checkUntreadActivity")
    public JsonResult<?> checkUntreadActivity(@RequestBody @Valid BackActPara para) {
        SysUser user = getUser();
        para.setUserId(user.userId);
        para.setUserName(user.userName);
        return eventInfoLogic.checkUntreadActivity(para);
    }

    @ApiOperation(value = "获取已办结案件列表")
    @PostMapping("/getFinishedEventList")
    public JsonResult<DataPager<EventInfoVO>> getFinishedEventList(@RequestBody EventInfoQuery query) {
        return JsonResult.ok(eventInfoLogic.getFinishedEventList(query, getUser()));
    }

    @ApiOperation("获取案件流程")
    @GetMapping("/getFlowsByIds")
    public JsonResult<List<ActivityInstanceVO>> getFlowsByIds(@RequestParam String wfInstId, @RequestParam String evtId) {
        return JsonResult.ok(eventInfoLogic.getFlowsByIds(wfInstId, evtId));
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<EventInfoVO> getInfoById(@RequestParam Integer id) {
        return JsonResult.ok(eventInfoLogic.getInfoById(id));
    }

    @ApiOperation("根据自增ID批量删除")
    @GetMapping("/deleteById")
    public JsonResult<Boolean> deleteBatchById(@RequestParam Integer id) {
        eventInfoLogic.delete(getUser().id.toString(), id);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出案件表信息")
    @GetMapping("/export")
    public void export(EventInfoQuery query, HttpServletResponse response) {
        List<EventInfoVO> list = eventInfoLogic.listPage(query, getUser()).records;
        ExcelUtil.exportExcel(list, "案件表", "sheet1", EventInfoVO.class, "案件表", response);
    }

    @ApiOperation("案件延时申请")
    @PostMapping("/eventDelayApply")
    public JsonResult<?> eventDelayApply(@Valid @RequestBody EventDelayApplyDTO para) {
        SysUser user = getUser();
        para.setUserId(user.userId);
        para.setDeptId(user.deptId);
        return eventInfoLogic.eventDelayApply(para);
    }

    @ApiOperation("案件延时审核")
    @PostMapping("/eventDelayAudit")
    public JsonResult<?> eventDelayAudit(@RequestBody @Valid EventDelayAuditDTO dto) {
        return eventInfoLogic.eventDelayAudit(getUser(), dto);
    }

    @ApiOperation("获取延时申请列表")
    @PostMapping("/getEventDelayApplyList")
    public JsonResult<DataPager<EventDelayApplyVO>> getEventDelayApplyList(@RequestBody EventDelayApplyQuery eventDelayApplyQuery) {
        return JsonResult.ok(eventInfoLogic.getEventDelayApplyList(eventDelayApplyQuery));
    }

    @ApiOperation("待处置任务和待巡查任务的统计（移动端）")
    @GetMapping("/getEventTaskCount")
    public JsonResult<List<Map<String, Object>>> getEventTaskCount() {
        return JsonResult.ok(eventInfoLogic.getEventTaskCount());
    }

    @ApiOperation("查询本人创建的案件")
    @GetMapping("/getOwnEventPage")
    public JsonResult<DataPager<EventInfoVO>> getOwnEventPage(@RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return JsonResult.ok(eventInfoLogic.getOwnEventPage(pageIndex, pageSize));
    }
}
